package com.nix.web.databases.service.iteratory;

import com.nix.web.databases.pojo.user.User;
import org.springframework.stereotype.Service;

/**
 * 用于遍历整个集合的类
 * Create by zhangpe0312@qq.com on 2017/12/12.
 */
@Service
public class ObjectAggregateIterator<T> implements Iterator<T> {

    private ObjectAggregate<T> aggregate;
    private int index;

    public ObjectAggregateIterator(ObjectAggregate<T> objectAggregate) {
        this.aggregate = objectAggregate;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        if (index < aggregate.getLength()) {
            return true;
        }
        return false;
    }

    @Override
    public T next() {
        T t = aggregate.indexOfValue(index);
        index++;
        return t;
    }

    /**
     * 迭代器使用方法
     * @param args
     */
    public static void main(String[] args) {
        Iterator<User> iterator;
        ObjectAggregate<User> list = new ObjectAggregate<User>();
        User user = new User();
        user.setName("zhangpei");
        user.setPassword("bisha520");
        list.append(user);
        User user1 = new User();
        user1.setName("bisha");
        list.append(user1);

        iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toString());
        }
    }
}
